package com.taxi.driver.module.order.list;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.qianxx.adapter.internal.SuperViewHolder;
import com.qianxx.utils.DateUtil;
import com.qianxx.view.refreshview.RefreshAdapter;
import com.taxi.driver.R;
import com.taxi.driver.config.DriverType;
import com.taxi.driver.config.OrderStatus;
import com.taxi.driver.module.vo.OrderSummaryVO;

import java.util.ArrayList;
import java.util.Date;

/**
 * 功能描述：
 */
public class OrderListAdapter extends RefreshAdapter<OrderSummaryVO> {

    private int driverType = DriverType.SPECIAL;

    public OrderListAdapter(Context context) {
        super(context, new ArrayList<>(), R.layout.item_order_list);
    }

    @Override
    public void onBind(SuperViewHolder holder, int viewType, int position, OrderSummaryVO item) {
        String curDate = DateUtil.formatDate(new Date(item.getDepartTime()), "MM月dd日");
        String curTime = DateUtil.formatDate(new Date(item.getDepartTime()), "HH:mm");
        if (position == 0) {
            holder.setVisibility(R.id.tv_date, View.VISIBLE);
        } else {
            OrderSummaryVO previous = mList.get(position - 1);
            String preDate = DateUtil.formatDate(new Date(previous.getDepartTime()), "MM月dd日");
            int vis = curDate.equals(preDate) ? View.GONE : View.VISIBLE;
            holder.setVisibility(R.id.tv_date, vis);
        }
        holder.setText(R.id.tv_date, curDate)
                .setText(R.id.tv_time, curTime)
                .setText(R.id.tv_start, item.getOriginAddress())
                .setText(R.id.tv_end, item.getDestAddress());

        TextView tvType = holder.getView(R.id.tv_type);
        int resId = OrderSummaryVO.tripType(item.typeTripNew, item.typeTimeNew);
        if (resId == 0) {
            tvType.setVisibility(View.GONE);
        } else {
            tvType.setText(resId);
            tvType.setVisibility(View.VISIBLE);
        }

        setStatusDisplay(holder, item); //设置状态显示
    }

    private void setStatusDisplay(SuperViewHolder holder, OrderSummaryVO item) {
        if (item.mainStatus == null || item.subStatus == null) return;
        String str = null;
        boolean highlight = false;

        switch (item.mainStatus) { //根据主状态判断显示
            case OrderStatus.ORDER_MAIN_STATUS_CANCEL: //订单取消（完结）
                str = "已取消";
                break;
            case OrderStatus.ORDER_MAIN_STATUS_PAYED: //支付完成、订单结束
                if (item.totalFare == null) {
                    str = "已支付";
                } else {
                    str = "¥" + String.format("%.01f", item.totalFare) + "  已支付";
                }
                break;
            case OrderStatus.ORDER_MAIN_STATUS_DONE: //行程结束、待支付
                if (item.totalFare == null) {
                    str = null;
                } else {
                    str = "¥" + String.format("%.01f", item.totalFare) + "  未支付";
                    highlight = true;
                }
                break;
            default:
                switch (item.subStatus) { //根据子状态判断显示
                    case OrderStatus.WAIT_BEGIN_APPOINTMENT:
                        str = "未开始";
                        highlight = true;
                        break;
                    case OrderStatus.WAIT_ARRIVE_ORIGIN:
                        if ( driverType == DriverType.SPECIAL
                               ) {
                            str = "等待接驾";
                        }
                        highlight = true;
                        break;
                    case OrderStatus.WAIT_PASSENGER_GET_ON:
                        if (driverType == DriverType.SPECIAL
                                ) {
                            str = "等待乘客上车";
                        }
                        highlight = true;
                        break;
                    case OrderStatus.DEPART:
                        str = "行程中";
                        highlight = true;
                        break;
                    case OrderStatus.ARRIVE_DEST:
                        if (item.totalFare == null) {
                            str = null;
                        } else {
                            str = "¥" + String.format("%.01f", item.totalFare) + "  未支付";
                            highlight = true;
                        }
                        break;
                }
                break;
        }
        TextView tvStatus = holder.getView(R.id.tv_status);
        if (str == null) {
            tvStatus.setVisibility(View.GONE);
        } else {
            tvStatus.setVisibility(View.VISIBLE);
            tvStatus.setText(str);
            tvStatus.setSelected(highlight);
        }
    }

    //设置司机类型
    public void setDriverType(int driverType) {
        this.driverType = driverType;
    }
}
